package zhcms.admin;


import java.io.UnsupportedEncodingException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import zhcms.db.DBManager;
import zhcms.db.DbConn;
import zhcms.db.ConnectionPool.PooledConnection;
import zhcms.framework.Action;
import zhcms.framework.ActionForward;
import zhcms.framework.GyReplace;
import zhcms.framework.GySplit;
import zhcms.tool.GyPage;

public class GyContent implements Action{

	String pageStr="";
	
	public GyContent()
	{
		
	}
	
	@Override
	public ActionForward excute(HttpServletRequest request,
			HttpServletResponse response) {
		return null;
	}
	
	public int listcount(){
		ResultSet rs = null;
		PooledConnection conn = null;
		String sql="select count(*) as id from gyhy_content";
		int countlyb=0;
		try {
			conn = DBManager.getConnection();
			rs = conn.executeQuery(sql);
			if (rs.next())
			{
				countlyb=rs.getInt("id");
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		return countlyb;
	}
	
	public ArrayList<HashMap<String, String>> getAllList(int currentpage)
	{
		int listnumcount=this.listcount();
		GyPage gyp=new GyPage();
		gyp.setP(15, listnumcount, "", "", currentpage);
		String limitStr=gyp.limit();
		pageStr=gyp.outPut();
		String sql="select  `Id` , `ColumnId` , `ColumnName` , `ColumnCode` , `Title` , `AddDate` , `Content` , `KeyWord` , `FromName` , `ViewNum` , `Byte` , `CommentNum` , `IsDel` , `Author` , `IsPass` , `TColor` , `IsImg` , `Img` , `IsTop` from gyhy_content where `IsDel`='0' order by Id desc limit "+limitStr;
		return DbConn.executeQuery(sql);
	}
	
	public ArrayList<HashMap<String, String>> getOneList(int postid)
	{
		String sql="select  `Id` , `ColumnId` , `ColumnName` , `ColumnCode` , `Title` , `AddDate` , `Content` , `KeyWord` , `FromName` , `ViewNum` , `Byte` , `CommentNum` , `IsDel` , `Author` , `IsPass` , `TColor` , `IsImg` , `Img` , `IsTop` from gyhy_content where `IsDel`='0' and Id='"+postid+"'";
		return DbConn.executeQuery(sql);
	}
	
	public ArrayList<HashMap<String, String>> getOneList(String postid)
	{
		String sql="select  `Id` , `ColumnId` , `ColumnName` , `ColumnCode` , `Title` , `AddDate` , `Content` , `KeyWord` , `FromName` , `ViewNum` , `Byte` , `CommentNum` , `IsDel` , `Author` , `IsPass` , `TColor` , `IsImg` , `Img` , `IsTop` from gyhy_content where `IsDel`='0' and Id='"+postid+"'";
		return DbConn.executeQuery(sql);
	}
	
	public String getPageStr() {
		return pageStr;
	}

	public void setPageStr(String pageStr) {
		this.pageStr = pageStr;
	}
	
	public ActionForward save(HttpServletRequest request, HttpServletResponse response)
	{
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("utf-8");
		} 
		catch (UnsupportedEncodingException e1) 
		{
			
		}
		String postid=getV(request.getParameter("postid"));
		//System.out.println("postid+++++++++++++"+postid);


		String Title=getV(request.getParameter("Title"));
   String Author=getV(request.getParameter("Author"));
   String Content=getV(request.getParameter("content"));
   //Content=GyHTMLEncode.htmlEncode(Content);
		//System.out.println("content+++++++++++++"+Content);
   String KeyWord=getV(request.getParameter("KeyWord"));
   //String OrderNum=getV(request.getParameter("OrderNum"));
   String TColor=getV(request.getParameter("TColor"));
   String IsImg=getV(request.getParameter("IsImg"));
   String ImgPath=getV(request.getParameter("ImgPath"));
   String IsTop=getV(request.getParameter("IsTop"));
   String ColumnCode=getV(request.getParameter("ColumnCode"));

   GySplit a = new GySplit();
		a.tool(ColumnCode, "|||");
		String[] tempColumnCode = a.Show();
		a = null;
		
		String ColumnId=tempColumnCode[0];
		String ColumnName=tempColumnCode[1];
		ColumnCode=tempColumnCode[2];
		
		PooledConnection conn = null;
		String sqlStr1="";
		String Byte=String.valueOf(Content.length());
		
		if(IsImg==null || IsImg.length()<=0 || IsImg.equals("null"))
		{
			IsImg="0";
		}
		
		if(IsTop==null || IsTop.length()<=0 || IsTop.equals("null"))
		{
			IsTop="0";
		}
		
		if(TColor==null || TColor.length()<=0 || TColor.equals("null"))
		{
			TColor="0";
		}
		
		if(ImgPath==null || ImgPath.length()<=0 || ImgPath.equals("null"))
		{
			ImgPath="";
		}
		
		if(KeyWord==null || KeyWord.length()<=0 || KeyWord.equals("null"))
		{
			KeyWord="";
		}
		
		
		if(postid==null || postid.length()<=0 || postid.equals("null"))
		{
			sqlStr1="insert into `gyhy_content` ( ColumnId , ColumnName , ColumnCode , Title , AddDate , Content , KeyWord , Byte , Author , IsPass, TColor , IsImg , Img , IsTop ,IsDis) values('"+ColumnId+"','"+ColumnName+"','"+ColumnCode+"','"+Title+"',now(),'"+Content+"','"+KeyWord+"','"+Byte+"','"+Author+"','1','"+TColor+"','"+IsImg+"','"+ImgPath+"','"+IsTop+"','0');";
		}
		else
		{
			sqlStr1="update `gyhy_content` set `ColumnId`='"+ColumnId+"' , `ColumnName`='"+ColumnName+"' , `ColumnCode`='"+ColumnCode+"' , `Title`='"+Title+"' , `Content`='"+Content+"' , `KeyWord`='"+KeyWord+"' , `Byte`='"+Byte+"' , `Author`='"+Author+"' , `TColor`='"+TColor+"' , `IsImg`='"+IsImg+"', `Img`='"+ImgPath+"',`IsTop`='"+IsTop+"' where `Id`="+postid;
		}
		
		//System.out.println(sqlStr1);
		boolean isok=false;
		try {
			conn = DBManager.getConnection();
			conn.executeUpdate(sqlStr1);
			conn.close();
			isok=true;
		} catch (SQLException e) {
			isok=false;
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		if(isok==true)
		{
			return new ActionForward("/admin/admin_content.jsp",3,"更新成功");
		}
		else
		{
			return new ActionForward("/admin/admin_content.jsp",3,"更新失败");
		}
	}
	
	public ActionForward del(HttpServletRequest request, HttpServletResponse response)
	{
		
		try {
			request.setCharacterEncoding("UTF-8");
			response.setCharacterEncoding("utf-8");
		} 
		catch (UnsupportedEncodingException e1) 
		{
			
		}
		String postid=getV(request.getParameter("postid"));
		//System.out.println("postid+++++++++++++"+postid);
		
		PooledConnection conn = null;
		String sqlStr1="";
		
		sqlStr1="delete from  `gyhy_content` where `Id`="+postid;
		
		//System.out.println(sqlStr1);
		boolean isok=false;
		try {
			conn = DBManager.getConnection();
			conn.executeUpdate(sqlStr1);
			conn.close();
			isok=true;
		} catch (SQLException e) {
			isok=false;
			e.printStackTrace();
		}
		finally {
            conn.close();
		}
		if(isok==true)
		{
			return new ActionForward("/admin/admin_content.jsp",3,"更新成功");
		}
		else
		{
			return new ActionForward("/admin/admin_content.jsp",3,"更新失败");
		}
	}
	
	public static String TransactSQLInjection(String str)
	{
		String returnS="";
		
		GyReplace r = new GyReplace();
		str = r.replace(str,"'", "‘");
		//str = r.replace(str,"\"", "“");
		str = r.replace(str,"-", "-");
		//str = r.replace(str,"<","《");
		//str = r.replace(str,">","》");
		str = r.replace(str,"select","Ｓelect");
		returnS=str;
		return returnS;
	 //return str.replaceAll(".*([';]+|(--)+).*", " ");
	}

	private String getV(String S)
	{
		//System.out.println(S);
		String returnS;
		if(S==null || S.length()<=0)
		{
			returnS="";
		}
		else
		{
			returnS=S;
		}
		return TransactSQLInjection(returnS);
	}
	

}
